package com.study.services;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.study.model.jdao.ICustomerDAO;
import com.study.model.jdao.IDeptDAO;
import com.study.model.jdao.IDictionaryDAO;
import com.study.model.jdao.IGoodDAO;
import com.study.model.jdao.ILinkmanDAO;
import com.study.model.jdao.IOfferDAO;
import com.study.model.jdao.IOpportunityDAO;
import com.study.model.jdao.IRepairlistDAO;
import com.study.model.jdao.IServiceDAO;
import com.study.model.jdao.IUserDAO;
import com.study.model.mdao.GoodDAO;
import com.study.model.mdao.LogforcustDAO;
import com.study.model.mdao.OfferDetailsDAO;
import com.study.model.mdao.OffersDAO;
import com.study.model.mdao.TousuDAO;
import com.study.model.mdao.UserMDAO;
import com.study.model.mdao.WeixiuDAO;
import com.study.model.pojo.Complaint;
import com.study.model.pojo.Good;
import com.study.model.pojo.Offer;
import com.study.model.pojo.Offersdetail;
import com.study.model.pojo.Repairlist;
import com.study.model.pojo.Service;
import com.study.model.vo.Cart;
import com.study.model.vo.CartBox;

@org.springframework.stereotype.Service
@Transactional(rollbackOn = Exception.class)
public class OffersService {

	@Autowired
	OffersDAO mapper;
	@Autowired
	ILinkmanDAO linkmanDAO;
	@Autowired
	ICustomerDAO customerDAO;
	@Autowired
	IUserDAO userDAO;
	@Autowired
	IDictionaryDAO dictionaryDAO;
	@Autowired
	IOfferDAO dao;
	@Autowired
	GoodDAO goodDAO;
	@Autowired
	IGoodDAO iGoodDAO;
	@Autowired
	IDeptDAO deptDAO;
	@Autowired
	OfferDetailsDAO detailsDAO;
	@Autowired
	IOpportunityDAO opportunityDAO;
	@Autowired
	LogforcustDAO logforcustDAO;

	@Autowired
	UserMDAO udao;

	/**
	 * 根据id查询一条记录
	 * 
	 * @param parameter
	 * @return
	 */
	public JSONObject selectBycid(String parameter) {
		JSONObject offer = mapper.selectById(parameter);
		//List<JSONObject> details = detailsDAO.selectAllById(parameter);
		return offer;
	}

	/**
	 * 删除
	 * 
	 * @param goo_id
	 */
	public void delete(String id) {
		// 只进行逻辑删除
		Offer offer = dao.findByOffId(Integer.parseInt(id));
		offer.setIsShow(0);
		List<Offersdetail> offersdetails = offer.getOffersdetails();
		for (Offersdetail offersdetail : offersdetails) {
			offersdetail.setIsShow(0);
		}
		
	}
	/**
	 * 添加
	 * @param theme
	 * @param custid
	 * @param linId
	 * @param dates
	 * @param price
	 * @param opp
	 * @param maoli
	 * @param user
	 * @param celve
	 * @param more
	 * @param cart
	 * @return
	 */
	public boolean addWeixiu(String theme, String custid, String linId, Date dates, String price, String opp,
			String maoli, String user, String celve, String more, CartBox cart) {
		 String name = udao.getfindUsreid(user).getString("use_name");
		System.out.println(price+"======="+maoli);
		if ("".equals(price)) {
			price = null;
		}
		if ("".equals(maoli) || maoli == null) {
			maoli = Integer.parseInt(price)*0.2+50+"";
		}
		if ("".equals(linId)) {
			linId = null;
		}
		if ("".equals(opp)) {
			opp = null;
		}
		if ("".equals(user) || user == null ) {
			user = 6+"";
		}
		int row = mapper.insert(opp, theme, dates, price, maoli, custid, user, linId, 1, more, celve);
		if (row > 0) {
			Integer id = mapper.findByName(theme);
			System.out.println(id);
			if (cart != null) {
				System.out.println("我有东西哦！");
				List<Cart> map = cart.getMap();
				for (Cart cart2 : map) {
					detailsDAO.insertinto(id, cart2.getGoods().getGooId(), cart2.getQuatity(),
							cart2.getGoods().getGooPrice(), cart2.getSum(),dates,user);
				}
			}
			String logMessage ="员工"+name+"在"+new Date()+"添加了一条报价信息";
			logforcustDAO.insert(Integer.parseInt(custid), new Date(), Integer.parseInt(user), 2202, logMessage, "");
		}
		
		return true;
	}
	/**
	 * 维修状态统计
	 * 
	 * @return
	 */
	public List<JSONObject> selectByResult() {
		return mapper.selectByResult();
	}

	/**
	 * 查询所有商品
	 * 
	 * @return
	 */
	public List<JSONObject> findGoods() {

		return goodDAO.selectAll();
	}
    /**
     * 查询当前报价是否转换为订单
     * @param id
     * @return
     */
	public JSONObject selectShow(String id) {
		return mapper.selectShow(id);
	}

	/**
	 * 编辑报价
	 * @param id
	 * @param theme
	 * @param custid
	 * @param linId
	 * @param dates
	 * @param price
	 * @param opp
	 * @param useid
	 * @param celve
	 * @param more
	 * @param id2
	 * @return
	 */
	public boolean editorService(String id, String theme, String custid, String linId, Date dates, Double price,
			String opp, String useid, String celve, String more) {
		String name = udao.getfindUsreid(useid).getString("use_name");
		boolean flag =false;
		     if (dates==null) {
		    	 flag= mapper.editor(id,theme,custid,linId,price,opp,useid,celve,more);
				
			}else {
				flag= mapper.editors(id,theme,custid,linId,dates,price,opp,useid,celve,more);
			}
		     if (flag) {
		    	 String logMessage ="员工"+name+"在"+new Date()+"修改了  " +theme + "报价信息";
					logforcustDAO.insert(Integer.parseInt(custid), new Date(), Integer.parseInt(useid), 222, logMessage, "");
			}
	return flag;
	}

		/**
	 * 根据条件查询报价
	 * @param cusid 
	 * 
	 * @param i
	 * @param j
	 * @param string
	 * @param string2
	 * @return
	 */

		public List<JSONObject> selectBySel(int pageNum, int pageSize, String input, Integer cusid, String useid) {
			PageHelper.startPage(pageNum, pageSize);
			
			if ("6".equals(useid)) {
				return mapper.selectByWhere("%" + input + "%", "%" + input + "%",cusid);
			}else {
				return mapper.selectByWheres("%" + input + "%", "%" + input + "%",cusid,useid);
			}
			
		}

}
